<template>
	<view class="container p30">
		<!-- ================================  顶部搜索  ==================================== -->
		<view class="search">
			<view>
				<input v-model="searchContent" type="text" placeholder="搜索你的需要">
			</view>
			<view @click="search">搜索</view>
		</view>
		<!-- ================================  热门搜索  ==================================== -->
		<view class="hotSearch">
			<view class="title">热门搜索</view>
			<view class="hotMsg">
				<view class="over" v-for="(item, index) in keyword" :key='index' @click="goSearch(item.keyword)">{{ item.keyword }}</view>
			</view>
		</view>
		<!-- ================================  历史记录  ==================================== -->
		<view class="history">
			<view class="title">
				<text>历史搜索</text>
				<image @click="delHistory" src="https://community.chuangxiangdianli.com/images/home/del_icon.png" mode=""></image>
			</view>
			<view class="historyMsg">
				<view class="over" v-for="(item, index) in history" :key='index' @click="goSearch(item.search_keyword)">{{ item.search_keyword }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				searchContent: '',
				history: [], // 搜索历史记录
				keyword: [], // 热词搜索
			}
		},
		onShow() {
			// 获取热词和历史搜索记录
			this.getHotwords()
		},
		methods: {
			//  删除搜索历史记录
			delHistory() {
				if (this.history.length == 0) return 
				// cosole.log('点击删除所有搜索历史记录')
				let that = this
				that.$mrequest({
					url: 'Mini/keywords/clearHistory',
					method: 'POST',
					callBack: function callBack (res) {
						if (res.status) {
							uni.showToast({
								title: '清空成功',
								icon: 'none'
							})
							that.getHotwords()
						}
					},
					fail: function fail (res) {
						
					}
				})
			},
			//  获取搜索历史记录和热词搜索
			getHotwords() {
				let that = this
				that.$mrequest({
					url: 'Mini/keywords/getList',
					method: 'POST',
					callBack: function callBack (res) {
						// console.log('获取成功', res)
						if (res.status) {
							that.keyword = res.data.keyword
							that.history = res.data.history
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					},
					fail: function fail (res) {
						// console.log('获取失败', res)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			//  点击热词、历史记录
			goSearch(str) {
				this.searchContent = str
				this.search()
			},
			// 点击查询
			search() {
				console.log('点击了查询！', this.searchContent)
				uni.navigateTo({
					url: '/other/classify/classifyDetail?searchText=' + this.searchContent
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		height: 80rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
		&>view:nth-child(1){
			flex: 1;
			height: 60rpx;
			border-radius: 30rpx;
			background-color: rgba(246, 246, 246, 1);
			padding: 0 30rpx;
			padding-left: 50rpx;
			box-sizing: border-box;
			&>input{
				height: 100%;
				width: 100%;
			}
		}
		&>view:nth-child(2){
			width: 120rpx;
			height: 100%;
			margin-left: 30rpx;
			border-radius: 30rpx;
			background: linear-gradient(-74deg, #70DAC4, #3EC4A7);
			text-align: center;
			line-height: 60rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
		
	}
	.hotSearch{
		width: 100%;
		// margin-top: 47rpx;
		.title{
			line-height: 60rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
		}
		.hotMsg{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			flex-wrap: wrap;
			width: 100%;
			&>view{
				height: 60rpx;
				background: #F6F6F6;
				border-radius: 30px;
				font-size: 30rpx;
				line-height: 60rpx;
				font-family: PingFang SC;
				font-weight: 500;
				padding: 0 30rpx;
				box-sizing: border-box;
				color: #34C39E;
				margin-right: 30rpx;
				margin-bottom: 20rpx;
			}
		}
	}
	.history{
		width: 100%;
		.title{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			line-height: 60rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
			&>image{
				display: block;
				height: 32rpx;
				width: 32rpx;
				margin-right: 40rpx;
			}
		}
		.historyMsg{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			flex-wrap: wrap;
			&>view{
				height: 60rpx;
				background: #F6F6F6;
				border-radius: 30px;
				padding: 0 30rpx;
				box-sizing: border-box;
				line-height: 60rpx;
				text-align: center;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
			}
		}
	}
</style>
